package com.quickdv.activity;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.quickdv.helper.ActivityImp;
import com.quickdv.helper.imp.ActivityInfo;
import com.quickdv.until.LogHelper;

import butterknife.ButterKnife;

public abstract class BaseActivity extends ActionBarActivity implements ActivityInfo {

    //为activity提供一些公用的事件和函数
    private ActivityImp acthelp;
    //    protected NetWorkImp network;
    protected ProgressDialog dialog;
    protected AlertDialog dialog_error;
    protected LogHelper log;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutID());
        log = LogHelper.getInstance();
        acthelp = new ActivityImp(this);
        initData();
        initView();
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        ButterKnife.inject(this);
    }

    @Override
    public View customActionBar() {
        return null;
    }

    /**
     * 是否是自定义的actionbar抬头
     */
    public void isBuildActionBar() {
        View actionBarCustomView = customActionBar();
        if (actionBarCustomView == null) return;
        getSupportActionBar().hide();

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getSupportActionBar().setDisplayShowHomeEnabled(false);

        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        initActionbar();
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish();
            default:
                return super.onOptionsItemSelected(item);
        }
    }


    public void startActivity(String action) {
        acthelp.startActivity(action);
    }

    public void startActivity(String action, Bundle bundle) {
        acthelp.startActivity(action, bundle);
    }

    public <T extends View> T findView(int id) {
        return (T) acthelp.findView(getWindow().getDecorView(), id);
    }

}
